package fr.utbm.gi.vi51.project.environment;

import java.util.ArrayList;
import java.util.Random;

import javax.vecmath.Point2d;

public class MapLoader {

	protected ArrayList<Map> mapList;

	public MapLoader() {
		this.mapList = new ArrayList<>();
		init();
	}

	public void init() {
		/* Map0 */
		Map map0 = new Map("default (no map)"); //$NON-NLS-1$

		this.mapList.add(map0);

		/* Map1 */
		Map map1 = new Map("map1"); //$NON-NLS-1$
		map1.add(new EnvironmentObject(32, 300, 300, EnvironmentType.MOUNTAIN, 32, 32));
		map1.add(new EnvironmentObject(32, 700, 300, EnvironmentType.MOUNTAIN, 32, 32));
		map1.add(new EnvironmentObject(32, 300, 400, EnvironmentType.MOUNTAIN, 32, 32));
		map1.add(new EnvironmentObject(32, 800, 650, EnvironmentType.MOUNTAIN, 32, 32));
		map1.add(new EnvironmentObject(32, 1000, 550, EnvironmentType.MOUNTAIN, 32, 32));
		map1.add(new EnvironmentObject(32, 752, 450, EnvironmentType.MOUNTAIN, 32, 32));
		map1.add(new EnvironmentObject(32, 450, 640, EnvironmentType.MOUNTAIN, 32, 32));
		map1.add(new EnvironmentObject(32, 450, 100, EnvironmentType.MOUNTAIN, 32, 32));
		map1.add(new EnvironmentObject(32, 428, 150, EnvironmentType.MOUNTAIN, 32, 32));
		map1.add(new EnvironmentObject(32, 550, 32, EnvironmentType.TREE, 32, 32));
		map1.add(new EnvironmentObject(32, 700, 270, EnvironmentType.TREE, 32, 32));
		map1.add(new EnvironmentObject(32, 450, 650, EnvironmentType.TREE, 32, 32));
		map1.add(new EnvironmentObject(32, 552, 486, EnvironmentType.TREE, 32, 32));
		map1.add(new EnvironmentObject(32, 345, 245, EnvironmentType.TREE, 32, 32));

		this.mapList.add(map1);

		/* map2 - Forest */
		Map map2 = new Map("map2"); //$NON-NLS-1$
		map2.add(new EnvironmentObject(32, 200, 50, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 300, 150, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 400, 50, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 500, 150, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 600, 50, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 700, 150, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 800, 50, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 250, 250, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 350, 350, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 450, 250, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 550, 350, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 650, 250, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 750, 350, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 850, 250, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 200, 450, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 300, 550, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 400, 450, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 500, 550, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 600, 450, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 700, 550, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 800, 450, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 250, 650, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 350, 750, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 450, 650, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 550, 750, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 650, 650, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 750, 750, EnvironmentType.TREE, 32, 32));
		map2.add(new EnvironmentObject(32, 850, 650, EnvironmentType.TREE, 32, 32));

		this.mapList.add(map2);

		/* map3 - superMapOfTheDoomBringer */
		Map map3 = new Map("Test Map"); //$NON-NLS-1$

		for (int i = 0; i < 12; ++i) {
			map3.add(new EnvironmentObject(32, 350, i * 32, EnvironmentType.TREE, 32, 32));
		}

		for (int i = 0; i < 10; ++i) {
			map3.add(new EnvironmentObject(32, 750, 700 - i * 32, EnvironmentType.COLUMN, 32, 32));
		}

		for (int i = 0; i < 5; ++i) {
			map3.add(new EnvironmentObject(32, 750 + i * 32, 412, EnvironmentType.COLUMN, 32, 32));
		}

		for (int i = 0; i < 15; ++i) {
			map3.add(new EnvironmentObject(32, 650 + i * 32, i * 10, EnvironmentType.MOUNTAIN, 32, 32));
		}

		map3.addWayPoint(new Point2d(1000, 600));
		map3.addWayPoint(new Point2d(1000, 300));
		map3.addWayPoint(new Point2d(600, 200));
		map3.addWayPoint(new Point2d(600, 600));
		map3.addWayPoint(new Point2d(100, 600));

		map3.addSpawnSpot(new Point2d(50, 750));
		map3.addSpawnSpot(new Point2d(1000, 600));
		map3.addSpawnSpot(new Point2d(800, 150));

		this.mapList.add(map3);

	}

	public void launchMapAtRandom() {
		Random r = new Random();
		this.mapList.get(r.nextInt(this.mapList.size())).launch();

	}

	public void launch(int index) {
		this.mapList.get(index).launch();
	}

	public Map get(int index) {
		return (this.mapList.get(index));
	}

	public int size() {
		return (this.mapList.size());
	}

	public ArrayList<Point2d> getWayPointList(int index) {
		return this.mapList.get(index).getWayPointList();
	}

	public Point2d getSpawnSpot(int mapIndex, int spotIndex) {
		return (this.mapList.get(mapIndex).getSpawnSpot(spotIndex));

	}

	public int indexOf(String _name) {
		int index = 0;

		for (int i = 0; i < this.mapList.size(); ++i) {
			if (get(i).getName().equals(_name)) {
				index = i;
			}
		}

		return index;
	}
}
